<template>
	<view class="page-body">
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<block v-for="(item, index) in menu" :key="index">
				<el-tab-pane :label="item.label" :name="item.sole"></el-tab-pane>
			</block>
		</el-tabs>
		
		<view class="form" style="max-width: 1450px;" v-loading="loading">
			<vk-data-form ref="form1" v-model="form1.data" :rules="form1.props.rules" :action="form1.props.action"
				:form-type="form1.props.formType" :columns='form1.props.columns' :show-cancel="false" submit-text="保存" label-width="140px"
				@success="form1.props.show = false;refresh();"></vk-data-form>
		</view>
	</view>
</template>

<script>
	var that;
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据
	var menu = [{
		sole: 'integral',
		name: '积分设置',
		label: '积分设置'
	}, {
		sole: 'vip',
		name: '会员设置',
		label: '会员设置'
	}]
	export default {
		data() {
			return {
				menu: [],
				activeName: "integral",
				form1: {},
				loading: true
			}
		},
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.initForm()

			let newsMenu = vk.getVuex('$app.config.platform');
			newsMenu = newsMenu.map(({value: sole, label}) => ({sole, label: label + '端设置'}))
			this.menu = menu.concat(newsMenu)
		},
		methods: {
			// 页面数据初始化函数
			async init() {
				let res = await vk.callFunction({
					url: 'admin/system_config/sys/getDetails',
					data: {
						type: that.activeName
					}
				});
				if(res.data) {
					that.form1.data = res.data;
				}
				setTimeout(() => {
					that.loading = false;
				}, 100)
			},
			
			// 刷新
			refresh() {},
			
			handleClick(tab, event) {
				// console.log(tab, event);
			},
			
			// 初始化表单
			initForm() {
				let activeName = this.activeName;
				if(activeName === "integral") {
					this.form1 = {
						// 请求表单数据, 在此可以设置默认值
						data: {
							type: "integral",
							invite_integral: "",
							invite_num: "",
							video_integral: "",
							video_num: "",
							sign_integral: "",
							register_integral: ""
						},
						// 表单属性
						props: {
							// 表单请求地址
							action: "admin/system_config/sys/add",
							// 表单字段显示规则
							columns: [
								{ key: "invite_integral", title: "邀请好友获得积分", type: "number" },
								{ key: "invite_num", title: "每日次数", type: "number", tips: "邀请好友每日次数限制，填写0为不限制" },
								{ key: "video_integral", title: "激励视频获得积分", type: "number" },
								{ key: "video_num", title: "每日次数", type: "number", tips: "激励视频每日次数限制，填写0为不限制，请注意这里的限制次数和作品视频次数不相关，一般设置5比较合理"},
								{ key: "sign_integral", title: "签到积分", type: "number", tips: "签到赠送积分，设置0为关闭"},
								{ key: "register_integral", title: "注册积分", type: "number", tips: "注册赠送积分，设置0为关闭"}
							]
						}
					}
				} else if(activeName === "vip") {
					that.form1 = {
						// 请求表单数据, 在此可以设置默认值
						data: {
							type: "vip",
							pay_way: 1,
							vip_list: [],
							vip_equity_list: [],
							extra_equity: ""
						},
						// 表单属性
						props: {
							// 表单请求地址
							action: "admin/system_config/sys/add",
							// 表单字段显示规则
							columns: [
								{ key: "vip_list", title: "会员套餐", type: "array<object>", showAdd:true, 
									defaultValue: { // 新增一行时,该行的默认值

									},
									rightBtns: ['delete'],
									// 每行每个字段对应的渲染规则
									columns: [
										{ key: "grade", title: "等级", type: "number", precision: 0, tips: "例：1, 2, 3" },
										{ key: "day", title: "天数", type: "number", precision: 0, tips: "例：30天为一月，360天为一年，大于等于18000天为永久；如果设置季度：天数 * 3=一季度" },
										{ key: "give", title: "赠送积分", type: "number", precision: 0, tips: "赠送为积分，不填/0为不赠送"},
										{ key: "price", title: "价格", type: "number", precision: 2 },
										{ key: "original_price", title: "原价", type: "number", precision: 2 }
									]
								},
								{ key: "vip_equity_list", title: "会员权益", type: "array<object>", showAdd:true,
									defaultValue: { // 新增一行时,该行的默认值
								
									},
									rightBtns: ['delete'],
									// 每行每个字段对应的渲染规则
									columns: [
										{ key: "grade", title: "等级", type: "number", precision: 0, tips: "对应会员套餐" },
										{ key: "name", title: "名称", type: "text", precision: 0 },
										{ key: "vip_equity", title: "会员权益", type: "discount", precision: 0}
									]
								},
								{ key: "extra_equity", title: "合伙人权益", type: "percentage", tips: "合伙人开通会员，增加额外分成"}
							]
						}
					}
				} else if(activeName === "mp-weixin") {
					that.form1 = {
						// 请求表单数据, 在此可以设置默认值
						data: {
							type: "mp-weixin",
							appName: "",
							appDesc: "方便快捷、安全可靠、资源丰富、个性化推荐~",
							appVerisons: "",
							appLogo: "",
							poster_back: "",
							service_code: "",
							trial: false,
							vipOpen: false,
							flowOpen: false,
							partnerOpen: false,
							partner_list: [{}, {}, {}],
							free_num: "",
							advertising_num: "",
							ad_banner: "",
							ad_interstitial: "",
							ad_incentive: "",
						},
						// 表单属性
						props: {
							// 表单请求地址
							action: "admin/system_config/sys/add",
							// 表单字段显示规则
							columns: [
								{ key: "", title: "基础配置", type: "bar-title" },
								{ key: "appName", title: "应用名称", type: "text" },
								{ key: "appDesc", title: "应用描述", type: "text" },
								{ key: "appVerisons", title: "应用版本", type: "text", },
								{ key: "appLogo", title: "LOGO", type: "file-select" },
								{ key: "poster_back", title: "海报背景", type: "file-select", tips: "分享海报背景，为空则默认不显示，建议上传" },
								{ key: "invite_back", title: "邀请背景", type: "file-select", tips: "邀请页背景，为空则默认不显示，建议上传"},
								{ key: "service_code", title: "客服二维码", type: "file-select", tips: "客服二维码，用于领取激活码" },
								{ key: "trial", title: "过审开关", type: "switch", tips: "开启后将不显示开启过审按钮的所有内容" },
								{ key: "vipOpen", title: "开启VIP", type: "switch", tips: "开启VIP功能则会员设置将生效" },
								{ key: "flowOpen", title: "开启流量主", type: "switch", tips: "开启流量主功能并开启流量主配置" },
								{ key: "partnerOpen", title: "开启合伙人", type: "switch", tips: "开启合伙人功能并开启三级分销配置"},
								{ key: "", title: "任务配置", type: "bar-title" },
								{ key: "advertising_num", title: "广告次数", type: "number", tips: "填写0不限制，每日广告下载作品次数，超过此次数之后将无法下载" },
								{ key: "free_num", title: "免费次数", type: "number", tips: "填写0不限制，每日免费下载作品次数，超过此次数之后将无法下载" },
								{ key: "give_num", title: "赠送次数", type: "number", tips: "填写0不限制，广告下载一定数量的作品后赠送的免费次数" },
								{ key: "give_rule", title: "赠送条件", type: "number", tips: "填写0不限制，赠送免费次数需要满足的条件，达到此次数之后赠送上方设置的次数" },
								{ key: "", title: "流量主配置", type: "bar-title",
									showRule: (formData) => {
										return formData.flowOpen
									}
								},
								{ key: "ad_banner", title: "banner广告", type: "text", tips: "配置此项需先开通流量主，设置空为关闭",
									showRule: (formData) => {
										return formData.flowOpen
									}
								},
								{ key: "ad_interstitial", title: "插屏广告", type: "text", tips: "配置此项需先开通流量主，设置空为关闭",
									showRule: (formData) => {
										return formData.flowOpen
									}
								},
								{ key: "ad_incentive", title: "激励广告", type: "text", tips: "配置此项需先开通流量主，设置广告生效",
									showRule: (formData) => {
										return formData.flowOpen
									}
								},
								{ key: "ad_video", title: "视频广告", type: "text", tips: "配置此项需先开通流量主，设置广告生效",
									showRule: (formData) => {
										return formData.flowOpen
									}
								},
								{ key: "", title: "合伙人配置", type: "bar-title",
									showRule: (formData) => {
										return formData.partnerOpen
									}
								},
								{ key: "partner_list", title: "分润配置", type: "array<object>", showAdd: true,
									defaultValue: { // 新增一行时,该行的默认值
										
									},
									rightBtns: ["delete"],
									// 每行每个字段对应的渲染规则
									columns: [
										{ key: "grade", title: "等级", type: "number", precision: 0, tips: "分销等级" },
										{ key: "name", title: "名称", type: "text", tips: "名称  例：一级活总监等"},
										{ key: "earnings", title: "直推分润", type: "percentage", precision: 0},
										{ key: "earnings_indirect", title: "间推分润", type: "percentage", precision: 0}
									],
									maxlength: 3,
									showRule: (formData) => {
										return formData.partnerOpen
									}
								},
								{ key: 'partner_upgrade', title: "升级分润", type: "percentage", precision: 0, tips: "用于级别升级，分润给上级比例，建议不超过10%",
									showRule: (formData) => {
										return formData.partnerOpen
									}
								},
								{ key: "", title: "钱包配置", type: "bar-title"},
								{ key: "partner_restrict", title: "提现限制", type: "number", precision: 2, tips: "填写0关闭，提现小于此数将无法提现 单位（元）" },
								{ key: "partner_release", title: "结算天数", type: "number", precision: 0, tips: "填写0关闭，用于收益自动释放 单位（天）"},
								{ key: "partner_service_fee", title: "提现手续费", type: "percentage", precision: 0, tips: "填写0关闭，用于用户提现手续费扣除比例"}
							],
							// 表单验证规则
							rules: {
								ad_banner: [
									{ required: true, message: "banner广告不能为空", trigger: ['blur','change'] }
								],
								ad_interstitial: [
									{ required: true, message: "插屏广告不能为空", trigger: ['blur','change'] }
								],
								ad_incentive: [
									{ required: true, message: "激励广告不能为空", trigger: ['blur','change'] }
								],
								ad_video: [
									{ required: true, message: "视频广告不能为空", trigger: ['blur','change'] }
								],
								partner_list: [
									{ required: true, message: "合伙人分润不能为空", trigger: ['blur','change'] }
								],
								partner_upgrade: [
									{ required: true, message: "合伙人升级分润不能为空", trigger: ['blur','change'] }
								]
							}
						}
					}
				}
				
				this.loading = true;
				that.init();
			}
		},
		watch: {
			activeName() {
				this.initForm()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form {
		width: 100%;
		padding: 15px 20px;
		-webkit-box-sizing: border-box;
		box-sizing: border-box;
	}
</style>